package com.JobWebGem.system.controller;

import com.JobWebGem.system.annotation.AuthCheck;
import com.JobWebGem.system.common.BaseResponse;
import com.JobWebGem.system.common.DeleteRequest;
import com.JobWebGem.system.common.ErrorCode;
import com.JobWebGem.system.common.ResultUtils;
import com.JobWebGem.system.constant.UserConstant;
import com.JobWebGem.system.exception.BusinessException;
import com.JobWebGem.system.exception.ThrowUtils;
import com.JobWebGem.system.model.dto.company.CompanyAddRequest;
import com.JobWebGem.system.model.dto.company.CompanyQueryRequest;
import com.JobWebGem.system.model.dto.company.CompanyUpdateRequest;
import com.JobWebGem.system.model.entity.Company;
import com.JobWebGem.system.model.entity.User;
import com.JobWebGem.system.model.vo.LoginUserVO;
import com.JobWebGem.system.model.vo.CompanyVO;
import com.JobWebGem.system.service.CompanyService;
import com.JobWebGem.system.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 公司信息接口
 *
 * @author renli
 */
@RestController
@RequestMapping("/company")
@Slf4j
@Api(tags = "公司信息接口")
public class CompanyController {

    @Resource
    private UserService userService;

    @Resource
    private CompanyService companyService;

    // region 增删改查

    /**
     * 创建公司信息
     *
     * @param companyAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "创建公司信息")
    public BaseResponse<Integer> addCompany(@RequestBody CompanyAddRequest companyAddRequest,
            HttpServletRequest request) {
        if (companyAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数为空");
        }
        Company company = new Company();
        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", companyAddRequest.getName());
        if (companyService.getOne(queryWrapper) != null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "公司名称已存在");
        }
        if (companyAddRequest.getListedStatus() == "未上市") {
            companyAddRequest.setListedStatus("0");
        }
        if (companyAddRequest.getListedStatus() == "上市") {
            companyAddRequest.setListedStatus("1");
        }
        BeanUtils.copyProperties(companyAddRequest, company);
        boolean result = companyService.save(company);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(company.getId());
    }

    /**
     * 删除公司信息
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "删除公司信息")
    public BaseResponse<Boolean> deleteCompany(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = companyService.removeById(deleteRequest.getId());
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "删除失败");
        return ResultUtils.success(result);
    }

    /**
     * 更新公司信息
     *
     * @param companyUpdateRequest
     * @param request
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "更新公司信息")
    public BaseResponse<Boolean> updateCompany(@RequestBody CompanyUpdateRequest companyUpdateRequest,
            HttpServletRequest request) {
        if (companyUpdateRequest == null || companyUpdateRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数为空");
        }
        Company company = new Company();
        BeanUtils.copyProperties(companyUpdateRequest, company);
        boolean result = companyService.updateById(company);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "更新失败");
        return ResultUtils.success(result);
    }

    /**
     * 分页获取全部公司信息
     *
     * @param companyQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page")
    @ApiOperation("分页获取全部公司信息")
    public BaseResponse<Page<CompanyVO>> listCompanyByPage(@RequestBody CompanyQueryRequest companyQueryRequest,
            HttpServletRequest request) {
        long current = companyQueryRequest.getCurrent() == 0 ? 1 : companyQueryRequest.getCurrent();
        long size = companyQueryRequest.getPageSize() == 0 ? 10 : companyQueryRequest.getPageSize();
        Page<Company> page = new Page<>(current, size);
        Page<CompanyVO> companyVOPage = companyService.getAll(page, companyQueryRequest);
        if (companyVOPage == null) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "查询失败，请联系管理员");
        }
        return ResultUtils.success(companyVOPage);
    }

    // endregion

}
